package com.indexingsystem.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.ExportThemeWordsReqVo;
import com.indexingsystem.boss.Vo.IdxThemeTablesListVo;
import com.indexingsystem.boss.Vo.IdxThemeTableslReqVo;
import com.indexingsystem.boss.admin.mapper.IdxThemeTableMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.IdxThemeTable;
import com.indexingsystem.boss.entity.IdxThemeTableWithBLOBs;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.service.IdxThemeTableService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class IdxThemeTableServiceImpl implements IdxThemeTableService {

	@Resource
	private IdxThemeTableMapper idxThemeTableMapper;

	@Override
	public int deleteByPrimaryKey(Integer themeTableId) {
		return idxThemeTableMapper.deleteByPrimaryKey(themeTableId);
	}

	@Override
	public int insert(IdxThemeTableWithBLOBs record) {
		return idxThemeTableMapper.insert(record);
	}

	@Override
	public int insertSelective(IdxThemeTableWithBLOBs record) {
		return idxThemeTableMapper.insertSelective(record);
	}

	@Override
	public IdxThemeTableWithBLOBs selectByPrimaryKey(Integer themeTableId) {
		return idxThemeTableMapper.selectByPrimaryKey(themeTableId);
	}

	@Override
	public int updateByPrimaryKeySelective(IdxThemeTableWithBLOBs record) {
		return idxThemeTableMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKeyWithBLOBs(IdxThemeTableWithBLOBs record) {
		return idxThemeTableMapper.updateByPrimaryKeyWithBLOBs(record);
	}

	@Override
	public int updateByPrimaryKey(IdxThemeTable record) {
		return idxThemeTableMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<IdxThemeTableWithBLOBs> selectThmeWordsByParentId(Integer parentId) {
		List<Integer> parentIdList = new ArrayList<Integer>();
		List<IdxThemeTableWithBLOBs> parentList = new ArrayList<IdxThemeTableWithBLOBs>();
		parentIdList.add( parentId );
		List<IdxThemeTableWithBLOBs> list = selectAllParentIdByParentId(parentList,parentIdList);
		return list;
	}

	/**
	 * 根据父级id查询所有父级id
	 */
	public List<IdxThemeTableWithBLOBs> selectAllParentIdByParentId(List<IdxThemeTableWithBLOBs> parentList, List<Integer> parentIdList) {
		List<IdxThemeTableWithBLOBs> themeTableWithBLOBsList = null;
		List<Integer> list = null;
		if (null != parentIdList && parentIdList.size() > 0) {
			themeTableWithBLOBsList = idxThemeTableMapper.selectByParentId( parentIdList );
			if (null != themeTableWithBLOBsList && themeTableWithBLOBsList.size() > 0) {
				for (IdxThemeTableWithBLOBs idxThemeTableWithBLOBs : themeTableWithBLOBsList) {
					list = new ArrayList<Integer>();
					parentList.add( idxThemeTableWithBLOBs );
					if (null != idxThemeTableWithBLOBs.getParentid() && idxThemeTableWithBLOBs.getParentid() != 0) {
						list.add( idxThemeTableWithBLOBs.getParentid() );
					} else {
						list.add( null );
					}
				}
			}
			selectAllParentIdByParentId( parentList, list );
		}
		return parentList;
	}

	@Override
	public Integer updateThmeTablesArrchildIdByParentId(Integer tablesId, Integer parentId) {
		Integer value = 0;
		StringBuffer sb = null;
		List<IdxThemeTableWithBLOBs> parentList = new ArrayList<IdxThemeTableWithBLOBs>();
		List<Integer> parentIdList = new ArrayList<Integer>();
		parentIdList.add( parentId );
		List<IdxThemeTableWithBLOBs> list = selectAllParentIdByParentId(parentList,parentIdList);
		if (null != list && list.size()>0) {
			for (IdxThemeTableWithBLOBs idxThemeTablesWithBLOBs : list) {
				sb = new StringBuffer();
				if (StringUtils.isNotBlank( idxThemeTablesWithBLOBs.getArrchildid() ) && !idxThemeTablesWithBLOBs.getArrchildid().equals( "0" )) {
					sb.append( idxThemeTablesWithBLOBs.getArrchildid() ).append( "," ).append( tablesId );
				}else{
					sb.append( tablesId );
				}
				idxThemeTablesWithBLOBs.setChild( 1 );
				idxThemeTablesWithBLOBs.setArrchildid(  sb.toString() );
				value = idxThemeTableMapper.updateByPrimaryKeySelective( idxThemeTablesWithBLOBs );
			}
		}
		return value;
	}

	@Override
	public List<IdxThemeTableWithBLOBs> getThemeTablesById(Integer themeTablesId) {
		return idxThemeTableMapper.getThemeTablesById( themeTablesId );
	}

	@Override
	public Integer deleteByIdBatch(List<String> list) {
		return idxThemeTableMapper.deleteByIdBatch( list );
	}

	@Override
	public Integer updateThemeTablesById(List<String> list) {
		Integer value = 0;
		List<IdxThemeTableWithBLOBs> idxThemeTablesListVos = idxThemeTableMapper.selectAllList();
		if (null != idxThemeTablesListVos && idxThemeTablesListVos.size()>0) {
			for (String wordId : list) {
				for (IdxThemeTableWithBLOBs idxThemeTablesListVo : idxThemeTablesListVos) {
					if (StringUtils.isNotBlank( idxThemeTablesListVo.getArrchildid())) {
						idxThemeTablesListVo.setArrchildid( idxThemeTablesListVo.getArrchildid().replace( ","+wordId, "" ).replace( wordId+",", "" ).replace( wordId, "" ));
					}
					if (idxThemeTablesListVo.getArrchildid().equals( String.valueOf( idxThemeTablesListVo.getThemeTableId() ) )) {
						idxThemeTablesListVo.setChild( 0 );
					}
					value = idxThemeTableMapper.updateByPrimaryKeySelective( idxThemeTablesListVo );
				}
			}
		}
		return value;
	}


	@Override
	public PagedResult<IdxThemeTablesListVo> selectIdxThemeTablel(IdxThemeTableslReqVo idxThemeTableslReqVo) {
		Integer pageNo = idxThemeTableslReqVo.getPageNo() == null ? 1 : idxThemeTableslReqVo.getPageNo();
		Integer pageSize = idxThemeTableslReqVo.getPageSize() == null ? 10 : idxThemeTableslReqVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( idxThemeTableMapper.selectIdxThemeTablel( idxThemeTableslReqVo ) );
	}

	@Override
	public Integer selectIdxThemeTableCount(IdxThemeTableslReqVo idxThemeTableslReqVo) {
		return idxThemeTableMapper.selectIdxThemeTableCount(idxThemeTableslReqVo);
	}

	@Override
	public String export(ExportThemeWordsReqVo reqVo, HSSFWorkbook wb) {

        String filenameSuffix = reqVo.getExportPath().split("\\.")[1];
        if (filenameSuffix.equals("xml")) {

        } else if (filenameSuffix.equals("xls")) {

        } else {
            return "保存文件类型错误";
        }

        return null;
	}


}
